package com.kanshu.ksgb.fastread.doudou.module.reader.page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by newbiechen on 17-7-1.
 */

public class TxtChapter{
    // 创建对象池
    private static final List<TxtChapter> sPool = new ArrayList<>();
    private static final int POOL_SIZE = 30;
    //章节所属的小说(网络)
    public String bookId;
    //章节的链接(网络)
    public String link;
    //章节名(共用)
    public String title;
    //章节索引
    public int chapterIndex;

    //章节内容在文章中的起始位置(本地)
    public long start;
    //章节内容在文章中的终止位置(本地)
    public long end;

    public String getBookId() {
        return bookId;
    }

    public void setBookId(String id) {
        this.bookId = id;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public long getStart() {
        return start;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public long getEnd() {
        return end;
    }

    public void setEnd(long end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return "TxtChapter{" +
                "title='" + title + '\'' +
                ", start=" + start +
                ", end=" + end +
                '}';
    }
    /**
     * 从对象池中获取对象
     * @return
     */
    public static TxtChapter obtain() {
        synchronized (sPool) {
            int size = sPool.size();
            if (size > 0) {
                return sPool.remove(sPool.size() - 1);
            }
        }
        return new TxtChapter();
    }

    /**
     * 将使用完的对象放入对象池中
     * @param page
     */
    public static void release(TxtChapter page) {
        synchronized (sPool) {
            for (TxtChapter txtPage : sPool) {
                if (page == txtPage) {
                    return;
                }
            }
            if (sPool.size() < POOL_SIZE) {
                sPool.add(page);
            }
        }
    }

    public void recycle() {
        release(this);
    }

}
